package com.sgb.member.view_model;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.google.gson.Gson;
import com.sgb.member.MemberConfig;
import com.sgb.member.entity.PeronMemberJoinEntity;
import com.sgb.member.entity.PersonMemberBean;
import com.sgb.member.weight.SimpleResult;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.view.selection.CommonPopEntity;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;

public class PersonInviteJoinModel2 extends ViewModel {
    public MutableLiveData<String> msgLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> isLoadLiveData = new MutableLiveData<>();
    public MutableLiveData<SimpleResult<CommonPopEntity>> simpleResultMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<PersonMemberBean> memberProfileLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> showEmptyViewLiviData = new MutableLiveData<>();
    public MutableLiveData<Boolean> memberJoinSuccessLiveData = new MutableLiveData<>();
    public MutableLiveData<List<CommonPopEntity>> levelLiveData = new MutableLiveData<>();
    /**
     * 加盟企业
     */
    public void memberJoin(PeronMemberJoinEntity entity) {
//        Log.i("xxx","xxx    "+new Gson().toJson(entity));
        isLoadLiveData.postValue(true);
        MemberConfig.getInstance().getApi().personMemberJoin(entity).enqueue(new ICallBack<BaseEntity<BaseEntity<String>>>() {
            @Override
            public void onSuccess(BaseEntity<BaseEntity<String>> data) {
                isLoadLiveData.postValue(false);
                if (data.getData().getCode().equals("0")) {
                    memberJoinSuccessLiveData.postValue(true);
                } else {
                    msgLiveData.postValue( data.getData().getMsg());
                }
            }
        });
    }

    //获取个人信息
    public void getPersonProfile(String no) {
        isLoadLiveData.postValue(true);
        MemberConfig.getInstance().getApi().getDetailsByUserNo(no).enqueue(new ICallBack<BaseEntity<PersonMemberBean>>() {
            @Override
            public void onSuccess(BaseEntity<PersonMemberBean> data) {
                isLoadLiveData.postValue(false);
                if(data != null &&  data.getData() != null){
                    memberProfileLiveData.postValue(data.getData());

                } else {
                    showEmptyViewLiviData.postValue(true);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<PersonMemberBean>> call, Throwable t) {
                super.onFailure(call, t);
                showEmptyViewLiviData.postValue(true);
            }
        });
    }


    // 会员等级
    public  void getMemberLevelList() {
        MemberConfig.getInstance().getApi().getMemberLevelList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                List<CommonPopEntity> tempList = data.getData();
                if (tempList != null && tempList.size() > 0) {
                    if (tempList.get(0).getTypeName().equals("全部")) {
                        tempList.remove(0);
                    }
                    List<CommonPopEntity>  mPop4 = new ArrayList<>();
                    mPop4.addAll(tempList);
                    levelLiveData.postValue(mPop4);
                }
            }
        });
    }

}
